<?php
/**
 * 服务标准化PC后台控制器
 */
namespace Beerus\Modules\DesignerService\Controllers;

use App\Http\Controllers\Controller;
use Beerus\Modules\DesignerService\Actions\ServiceFail;
use Beerus\Modules\DesignerService\Actions\ServicePass;
use Doraemon\model\DesignerOperatorBind\DesignerOperatorBind;
use Doraemon\model\DesignerService\ServiceContentRecord;
use Doraemon\model\DesignerService\DesignerServiceSpot;
use Doraemon\service\DesignerService\DesignerService as sDesignerService;
use Doraemon\model\Member;
use Doraemon\service\DesignerService\ServiceSpotService;
use Doraemon\tools\Hhzurl;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;

class ServiceContentController extends Controller
{

    const PAGE_SIZE = 20;

    // 服务内容管理
    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);
        $params = $request->all();

        $condition = [];
        if (!empty($params['uid'])) {
            $condition['uid'] = $params['uid'];
        }
        if (!empty($params['nick'])) {
            $uid = Member::get('uid', ['nick' => $params['nick']]);
            if ($uid) {
                $condition['uid'] = $uid;
            }
        }
        if (!empty($params['audit_status'])) {
        	$condition['audit_status'] = $params['audit_status'];
        }

        $total = ServiceContentRecord::getAllPublishDesignerCnt($condition);
        $lists = ServiceContentRecord::getAllPublishDesigner($condition, $page, $page_size);
        $this->formatList($lists);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->perPages([10, 20, 30, 50, 100, 500]);
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', 'UID')->placeholder('请输入设计师UID');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('nick', '设计师昵称')->placeholder('请输入设计师昵称');
                    });
                    $filter->expand();
                });

                $grid->column('id', 'ID');
                $grid->column('uid', '设计师UID');
                $grid->column('nick', '设计师昵称');
                $grid->column('operator_admin_name', '对接运营');
                $grid->column('spot_name', '亮点标签')->display(function () {
                    return $this->spot_name;
                });
                $grid->column('add_time', '首次设置时间');
                $grid->column('edit_time', '最近一次提交时间');
                $grid->column('remain_times', '当月剩余修改次数');
                $grid->column('history_times', '历史修改次数')->expand(function ($model) {
                    $list = ServiceContentRecord::select(['id', 'add_time', 'audit_status'],
                        [
                            'uid' => $this->uid,
                            'data_type' => ServiceContentRecord::TYPE_PUBLISHED,
                        ]);
                    foreach ($list as &$item) {
                        $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
                        //$item['add_time'] = "<a href='http://www.baidu.com' style='text-decoration:underline'>{$item['add_time']}</a>";
	                    $link = Hhzurl::getDomain() . "/designer/service-info?source=3&id={$item['id']}";
                        $item['add_time'] = '<a class="fa " href="javascript:layer.open({
                            type: 2,
                            title: \'预览\', 
                            maxmin: true, 
                            area: [\'600px\', \'800px\'],
                            fixed: false,
                            offset: \'100px\',
                            content: \'' . $link . '\' 
	                    });">' . htmlentities($item['add_time']) . '</a>';
                        $item['audit_status'] = ServiceContentRecord::AUDIT_TEXT[$item['audit_status']];
                    }
                    unset($item);

                    return new Table(['ID', '发布时间', '审核状态'], $list);
                }, true);

	            $grid->actions(function (Grid\Displayers\Actions $actions) {
		            $actions->disableDelete();
		            $actions->disableEdit();
		            $actions->disableView();
	            });
	            $grid->disableActions();
            }
        );

        $tab = new Tab();
        $tab->add('服务内容管理', $gridList->render());
        $content->row($tab);
        return $content
            ->header('服务内容管理')
            ->description(' ')
            ->breadcrumb(['text' => '服务内容管理']);
    }

    public function formatList(&$list) {
        foreach ($list as &$item) {
            $item['nick'] = Member::get('nick', ['uid' => $item['uid']]);
            $item['operator_admin_name'] = DesignerOperatorBind::getOperatorAdminNameByUid($item['uid']) ?? '未认领';
            $selectedSpotList = DesignerServiceSpot::getSpotListByStatus($item['uid'], DesignerServiceSpot::SELECTED);
            $item['spot_name'] = $selectedSpotList ? ServiceSpotService::getDisplaySpotName($selectedSpotList) : '未设置';
            $item['add_time'] = date('Y-m-d H:i:s', ServiceContentRecord::getFirstPublishTime($item['uid']));
            $item['edit_time'] = date('Y-m-d H:i:s', ServiceContentRecord::getLastPublishTime($item['uid']));
            $item['remain_times'] = sDesignerService::canReleaseNum($item['uid']);
            $item['history_times'] = sDesignerService::getAllPublishTimes($item['uid']);
        }
        unset($item);
    }
}
